<template>
  <div class="advanced-setting" v-if="settionShow">
    <!-- 新增批记录项 Start -->
    <a-tabs default-active-key="1" tab-position="top">
      <a-tab-pane key="1" tab="基本设置">
        <h3 style="text-align: center">
          工作项名称:{{ settionItem.JobItemName }}
        </h3>
        <!-- 基本设置 Start -->
        <basic-seettings
          :info="basicSettings"
          :settionItem="settionItem"
        ></basic-seettings>
        <!-- 基本设置 End -->
      </a-tab-pane>
      <a-tab-pane key="2" tab="条件约束">
        <h3 style="text-align: center">
          工作项名称:{{ settionItem.JobItemName }}
        </h3>
        <!-- 条件约束 Start -->
        <condition :info="condition" :settionItem="settionItem"></condition>
        <!-- 条件约束 End -->
      </a-tab-pane>
      <a-tab-pane
        key="3"
        tab="关联显示"
        v-if="
          settionItem.DataTypeName == '单选' ||
          settionItem.DataTypeName == '填空（字符）' ||
          settionItem.DataTypeName == '填空（数值）'
        "
      >
        <h3 style="text-align: center">
          工作项名称:{{ settionItem.JobItemName }}
        </h3>
        <!-- 条件约束 Start -->
        <associationShow
          :info="associationInfo"
          :settionItem="settionItem"
        ></associationShow>
        <!-- 条件约束 End -->
      </a-tab-pane>
      <a-tab-pane key="4" tab="结果脚本">
        <h3 style="text-align: center">
          工作项名称:{{ settionItem.JobItemName }}
        </h3>
        <!-- 结果脚本 Start -->
        <scenario :info="scenario" :settionItem="settionItem"></scenario>
        <!-- 结果脚本 End -->
      </a-tab-pane>
      <a-tab-pane key="5" tab="默认值脚本">
        <h3 style="text-align: center">
          工作项名称:{{ settionItem.JobItemName }}
        </h3>
        <!-- 默认值脚本 Start -->
        <default-value
          :info="defaultValue"
          :settionItem="settionItem"
        ></default-value>
        <!-- 默认值脚本 End -->
      </a-tab-pane>
      <a-tab-pane key="6" tab="计算脚本" :forceRender="true">
        <h3 style="text-align: center">
          工作项名称:{{ settionItem.JobItemName }}
        </h3>
        <!-- 计算脚本 Start -->
        <calculation-script
          :info="calculation"
          :settionItem="settionItem"
          :settionList="settionList"
        ></calculation-script>
        <!-- 计算脚本 End -->
      </a-tab-pane>
    </a-tabs>
    <!-- 新增批记录项 End -->
    <div class="footer">
      <a-button @click="onClose"> 取消 </a-button>
      <a-button class="default-button" @click="saveBtn"> 提交 </a-button>
    </div>
  </div>
</template>

<script>
import {
  getItemtLimiteds,
  updateItemtLimiteds,
  addItemtLimiteds,
} from "@/api/ItemtLimited"; //条件约束请求
import {
  getJobItemShowLimited,
  insItemShowLimited,
  updItemShowLimited,
} from "@/api/JobItemShowLimited"; //条件约束请求
import {
  updateJobFieldDataScripts,
  addJobFieldDataScripts,
  getJobFieldDataScripts,
} from "@/api/JobFieldDataScript"; //结果脚本和默认值脚本
import {
  getCalExcuteScripts,
  addCalExcuteScripts,
  updateCalExcuteScripts,
} from "@/api/CalExcuteScript";
import {
  getCalExcuteScriptDetails,
  updateCalExcuteScriptDetails,
  addCalExcuteScriptDetails,
} from "@/api/CalExcuteScriptDetail";
import { modifyJobItemConstraint } from "@/api/JobItem";
import BasicSeettings from "../settion-box/basic-settings.vue"; //基本设置
import Condition from "../settion-box/condition.vue"; //条件约束
import Scenario from "../settion-box/scenario.vue"; //结果脚本
import DefaultValue from "../settion-box/default-value.vue"; //默认值脚本
import CalculationScript from "../settion-box/calculation-script.vue"; //计算脚本
import AssociationShow from "../settion-box/association-show.vue"; //关联显示
export default {
  props: ["settionShow", "settionItem", "settionList"],
  components: {
    BasicSeettings,
    Condition,
    Scenario,
    DefaultValue,
    CalculationScript,
    AssociationShow,
  },
  data() {
    return {
      basicSettings: {
        isExecute: false, //是否执行
        itemDecimalDigits: null, //小数位数
        isReadScale: false, //是否读称
        isScan: false, //是否扫码
      }, //基本设置
      condition: {
        isExecute: false, //是否执行
        conditionList: [], //条件约束
      }, //条件约束
      scenario: {
        isExecute: false, //是否执行
        scenarioList: [], //结果脚本
      }, //结果脚本
      defaultValue: {
        isExecute: false, //是否执行
        defaultValueList: [], //默认值脚本
      }, //默认值脚本
      calculation: {
        isExecute: false, //是否执行
        calculateFunctionID: null, //计算函数的ID
        calculateSciptID: null, //新建后计算脚本的ID
        calculationNumList: [], //计算脚本计算数据
        selectedCalculationNumList: [], //选中的数据
        calculationTypeList: [], //计算脚本计算类型
        calculationResList: [], //计算脚本计算结果
      }, //计算脚本
      associationInfo: {
        isExecute: false, //是否执行
        associationList: [], //关联显示
        calculationNumList: [], //选中的数据
      }, //关联显示
      currentJobItemID: "", //当前id
    };
  },
  methods: {
    //关闭
    onClose() {
      this.understandData();
      this.$emit("update:settionShow", false);
    },
    //保存
    async saveBtn() {
      /* 条件约束 */
      let { conditionList, isExecute: conditionIsExecute } = this.condition;
      if (conditionIsExecute) {
        let oldconditionList = [];
        let newconditionList = [];
        conditionList.map(async (citm) => {
          citm.JobItemID = this.currentJobItemID;
          if (citm.ItemtLimitedID) {
            oldconditionList.push(citm);
          } else {
            newconditionList.push(citm);
          }
        });
        if (newconditionList.length > 0) {
          let addRes = await addItemtLimiteds(newconditionList);
        }
        if (oldconditionList.length > 0) {
          let editRes = await updateItemtLimiteds(oldconditionList);
        }
      }

      /* 关联显示 */
      let { associationList, isExecute: associationExecute } =
        this.associationInfo;
      if (associationExecute) {
        let oldassociationList = [];
        let newassociationList = [];
        associationList.map(async (citm) => {
          citm.JobItemID = this.currentJobItemID;
          if (citm.jobItemShowLimitedID) {
            oldassociationList.push(citm);
          } else {
            newassociationList.push(citm);
          }
        });
        if (newassociationList.length > 0) {
          let addRes = await insItemShowLimited(newassociationList);
        }
        if (oldassociationList.length > 0) {
          let editRes = await updItemShowLimited(oldassociationList);
        }
      }

      /* 结果脚本 */
      let { scenarioList, isExecute: scenarioIsExecute } = this.scenario;
      if (scenarioIsExecute) {
        let oldscenarioList = [];
        let newscenarioList = [];

        scenarioList.map(async (citm, cidx) => {
          citm.JobItemID = this.currentJobItemID;
          delete citm.DataFieldName;
          if (citm.JScriptID) {
            oldscenarioList.push(citm);
          } else {
            citm.BusinessType = 2;
            citm.JScriptIndex = cidx + 1;
            newscenarioList.push(citm);
          }
        });
        if (newscenarioList.length > 0) {
          let addRes = await addJobFieldDataScripts(newscenarioList);
        }
        if (oldscenarioList.length > 0) {
          let editRes = await updateJobFieldDataScripts(oldscenarioList);
        }
      }

      /* 默认值脚本 */
      let { defaultValueList, isExecute: defaultValueIsExecute } =
        this.defaultValue;
      if (defaultValueIsExecute) {
        let olddefaultValueList = [];
        let newdefaultValueList = [];

        defaultValueList.map(async (citm, cidx) => {
          citm.JobItemID = this.currentJobItemID;
          delete citm.DataFieldName;
          if (citm.JScriptID) {
            olddefaultValueList.push(citm);
          } else {
            citm.BusinessType = 1;
            citm.JScriptIndex = cidx + 1;
            newdefaultValueList.push(citm);
          }
        });
        if (newdefaultValueList.length > 0) {
          let addRes = await addJobFieldDataScripts(newdefaultValueList);
        }
        if (olddefaultValueList.length > 0) {
          let editRes = await updateJobFieldDataScripts(olddefaultValueList);
        }
      }

      /* 计算脚本 */
      let {
        isExecute: calculationIsExecute,
        calculationTypeList,
        calculationResList,
        calculateFunctionID,
        selectedCalculationNumList,
      } = this.calculation;
      if (calculationIsExecute) {
        let obj = {
          JobItemID: this.currentJobItemID,
          CalculateFunction: calculateFunctionID,
          CalculateData: selectedCalculationNumList.join(","),
        };
        if (this.calculation.calculateSciptID) {
          obj.ScriptID = this.calculation.calculateSciptID;
          let res = await updateCalExcuteScripts([obj]);
        } else {
          let res = await addCalExcuteScripts([obj]);
        }
        this.getCalExcuteScriptsFun(this.currentJobItemID);
        let oldCalculationTypeList = []; //已有id的计算类型
        let newoldCalculationTypeList = []; //无id的计算类型
        let oldCalculationResList = []; //已有id的计算结果
        let newCalculationResList = []; //无id的计算结果
        // 计算类型
        calculationTypeList.map(async (citm, cidx) => {
          citm.JobItemID = this.currentJobItemID;
          citm.ScriptID = this.calculation.calculateSciptID;
          if (citm.ScriptDetailID) {
            oldCalculationTypeList.push(citm);
          } else {
            citm.ScriptType = 10;
            citm.JScriptIndex = cidx + 1;
            newoldCalculationTypeList.push(citm);
          }
        });
        if (newoldCalculationTypeList.length > 0) {
          let addRes = await addCalExcuteScriptDetails(
            newoldCalculationTypeList
          );
        }
        if (oldCalculationTypeList.length > 0) {
          let editRes = await updateCalExcuteScriptDetails(
            oldCalculationTypeList
          );
        }
        // 计算结果
        calculationResList.map(async (citm, cidx) => {
          citm.JobItemID = this.currentJobItemID;
          citm.ScriptID = this.calculation.calculateSciptID;
          if (citm.ScriptDetailID) {
            oldCalculationResList.push(citm);
          } else {
            citm.ScriptType = 20;
            citm.JScriptIndex = cidx + 1;
            newCalculationResList.push(citm);
          }
        });
        if (newCalculationResList.length > 0) {
          let addRes = await addCalExcuteScriptDetails(newCalculationResList);
        }
        if (oldCalculationResList.length > 0) {
          let editRes = await updateCalExcuteScriptDetails(
            oldCalculationResList
          );
        }
      }

      /* 约束条件 */
      let factorData = {
        JobItemID: this.currentJobItemID,
        MustRecord: this.basicSettings.isExecute,
        IsReadScale: this.basicSettings.isReadScale,
        IsScan: this.basicSettings.isScan,
        ItemDecimalDigits: this.basicSettings.itemDecimalDigits,
        IsLimited: this.condition.isExecute,
        IsCalculate: this.calculation.isExecute,
        IsDynamicResultGroup: this.scenario.isExecute,
        IsDynamicDefault: this.defaultValue.isExecute,
        IsShow: this.associationInfo.isExecute,
      };

      let constraintRes = await modifyJobItemConstraint(factorData);
      this.onClose();
      this.$message.success("保存成功");
      this.$emit("upTerm", this.settionItem);
    },
    //还原默认参数
    understandData() {
      this.basicSettings = {
        isExecute: false, //是否执行
        itemDecimalDigits: null, //小数位数
        isReadScale: false, //是否读称
        isScan: false, //是否扫码
      }; //基本设置
      this.condition = {
        isExecute: false, //是否执行
        conditionList: [], //条件约束
      }; //条件约束
      this.scenario = {
        isExecute: false, //是否执行
        scenarioList: [], //结果脚本
      }; //结果脚本
      this.defaultValue = {
        isExecute: false, //是否执行
        defaultValueList: [], //默认值脚本
      }; //默认值脚本
      this.calculation = {
        isExecute: false, //是否执行
        calculateFunctionID: null, //计算函数的ID
        calculateSciptID: null, //新建后计算脚本的ID
        calculationNumList: [], //计算脚本计算数据
        selectedCalculationNumList: [], //选中的数据
        calculationTypeList: [], //计算脚本计算类型
        calculationResList: [], //计算脚本计算结果
      }; //计算脚本
      this.associationInfo = {
        isExecute: false, //是否执行
        associationList: [], //关联显示
        calculationNumList: [], //选中的数据
      }; //关联显示
    },
    //获取条件约束
    async getItemtLimitedsFun(JobItemID) {
      let res = await getItemtLimiteds({
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 1000,
        },
        JobItemID,
      });
      this.condition.conditionList = res.Data.Data;
    },
    //获取关联显示
    async getJobItemShowLimitedFun(JobItemID) {
      let res = await getJobItemShowLimited({
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 1000,
        },
        JobItemID,
      });
      this.associationInfo.associationList = res.Data.Data;
    },
    //获取脚本 结果 BusinessType=2  默认值  BusinessType=1
    async getJobFieldDataScriptsFun(JobItemID, BusinessType) {
      let res = await getJobFieldDataScripts({
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 1000,
        },
        JobItemID,
        BusinessType,
      });
      if (BusinessType == 2) {
        this.scenario.scenarioList = res.Data.Data;
      } else {
        this.defaultValue.defaultValueList = res.Data.Data;
      }
    },
    //获取计算脚本
    async getCalExcuteScriptsFun(JobItemID) {
      let res = await getCalExcuteScripts({
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 1000,
        },
        JobItemID,
      });
      if (res.Data.Data[0]) {
        let { ScriptID, CalculateDatas, CalculateFunction } = res.Data.Data[0];
        this.calculation = {
          ...this.calculation,
          calculateSciptID: ScriptID,
          selectedCalculationNumList: CalculateDatas,
          calculateFunctionID: CalculateFunction,
        };
        this.getCalExcuteScriptDetailsFun(ScriptID, 10);
        this.getCalExcuteScriptDetailsFun(ScriptID, 20);
      }
    },
    //获取计算脚本详情
    async getCalExcuteScriptDetailsFun(ScriptID, ScriptType) {
      let res = await getCalExcuteScriptDetails({
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 1000,
        },
        ScriptID,
        ScriptType,
      });
      //10为计算过程
      if (ScriptType == 10) {
        this.calculation.calculationTypeList = res.Data.Data;
      } else {
        this.calculation.calculationResList = res.Data.Data;
      }
    },
  },
  watch: {
    settionShow: {
      handler(newValue, oldValue) {
        if (newValue) {
          let {
            MustRecord,
            IsLimited,
            JobItemID,
            IsCalculate,
            IsDynamicResultGroup,
            IsDynamicDefault,
            IsShow,
            ItemDecimalDigits,
            IsScan,
            IsReadScale,
          } = this.settionItem;
          this.currentJobItemID = JobItemID;
          this.getItemtLimitedsFun(JobItemID);
          this.getJobItemShowLimitedFun(JobItemID);
          this.getJobFieldDataScriptsFun(JobItemID, 2);
          this.getJobFieldDataScriptsFun(JobItemID, 1);
          this.getCalExcuteScriptsFun(JobItemID);
          this.basicSettings.isExecute = MustRecord; //是否必填
          this.basicSettings.itemDecimalDigits = ItemDecimalDigits; //小数位数
          this.basicSettings.isReadScale = IsReadScale; //是否必填
          this.basicSettings.isScan = IsScan; //小数位数
          this.condition = {
            ...this.condition, //是否约束
            isExecute: IsLimited,
          };
          this.scenario = {
            ...this.scenario,
            isExecute: IsDynamicResultGroup,
          }; //结果
          this.defaultValue = {
            ...this.defaultValue,
            isExecute: IsDynamicDefault,
          }; //默认值
          this.calculation = {
            ...this.calculation,
            isExecute: IsCalculate,
          }; //是否计算
          this.calculation.calculationNumList = this.settionList.filter(
            (itm) => itm.JobItemID
          );
          this.calculation.calculationNumList.map((ditm) => {
            ditm.status = false;
          });
          //
          this.associationInfo = {
            ...this.associationInfo,
            isExecute: IsShow,
          };
          this.associationInfo.calculationNumList = this.settionList.filter(
            (itm) => itm.JobItemID
          );
        }
      },
      immediate: true, // 重点
      deep: true, // 重点
    },
  },
};
</script>

<style lang="less" scoped>
.advanced-setting {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 3;
  background: #ffffff;

  .ant-tabs {
    height: calc(100% - 2.5rem);
    padding: 0.625rem;

    /deep/ .ant-tabs-content {
      height: calc(100% - 4rem);

      .ant-tabs-tabpane {
        height: 100%;
      }
    }
  }

  .footer {
    display: flex;
    justify-content: flex-end;

    .ant-btn {
      margin-right: 1.25rem;
    }
  }
}
</style>
